﻿using Core.IService;
using Core.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.WebApi.Controllers.SystemControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictionaryController : ControllerBase
    {
        private readonly ISysDictionaryTypeService typeService;
        private readonly ISysDictionaryService dictService;

        public DictionaryController(ISysDictionaryTypeService typeService, ISysDictionaryService dictService)
        {
            this.typeService = typeService;
            this.dictService = dictService;
        }

        /// <summary>
        /// 根据字典类型查询字典值
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SysDictionary>>> GetList(string typeName)
        {
            var list= await dictService.GetListByTypeAsync(typeName);
            return list;
        }

        [HttpPost]
        public async Task<ActionResult<SysDictionary>> Post(SysDictionary dict)
        {
            await  dictService.AddAsync(dict);
            return CreatedAtAction("", new { Id = dict.Id }, dict);
        }

        [HttpPut]
        public async Task<IActionResult> Put(SysDictionary dict)
        {
            await dictService.EditAsync(dict);
            return NoContent();
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<SysDictionary>> Get(int id)
        {
            var dict= await dictService.GetEntityAsync(id);
            if (dict == null)
                return NotFound();
            return dict;
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            await dictService.DeleteAsync(id);
            return NoContent();
        }


    }
}
